package org.example.com.Controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.com.service.ShoppingCartService;
import org.example.com.util.JsonResult;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cart")
public class ShoppingCartController {
    @Resource
    private ShoppingCartService shoppingCartService;
    @PostMapping("/add/{id}")
    public JsonResult add(@PathVariable("id") String bookId, HttpServletRequest request) {
        String token = request.getHeader("token");
        return shoppingCartService.add(bookId, token);
    }
    @GetMapping("/find")
    public JsonResult find(HttpServletRequest request) {
        String token = request.getHeader("token");
        return shoppingCartService.find(token);
    }
    @PutMapping("/change_cart_number/{bookId}/{number}")
    public JsonResult changeCartNumber(HttpServletRequest request,@PathVariable("bookId") String bookId, @PathVariable("number") int number) {
        String token = request.getHeader("token");
        return shoppingCartService.changeCartNumber(token, bookId, number);
    }
    @PutMapping("/change_cart_number2/{bookId}/{number}")
    public JsonResult changeCartNumber2(HttpServletRequest request,@PathVariable("bookId") String bookId, @PathVariable("number") int number) {
        String token = request.getHeader("token");
        return shoppingCartService.changeCartNumber(token, bookId, number);
    }
    @DeleteMapping("/remove/{bookId}")
    public JsonResult remove(HttpServletRequest request, @PathVariable("bookId") String bookId) {
        String token = request.getHeader("token");
        return shoppingCartService.removeCartById(token, bookId);
    }
    @DeleteMapping("/batch_remove")
    public JsonResult batchRemoveCart(HttpServletRequest request,@RequestParam("ids") String[] ids) {
        String token = request.getHeader("token");
        return shoppingCartService.batchRemoveCate(token, ids);
    }
}
